import PAGE from '../../utils/middleware/page'
Page(PAGE({
    data: {
        loaded: false,
        list: [],
        apiField: {
            module: 'common',
            api: 'ParkList',
            data: 'list',
            search: {
                Keywords: null,
                Longitude: null,//经度
                Latitude: null, //纬度
            }
        },
        redirectType: 0, //调整类型 0：办理包月 1绑定电卡
        memberId: '', //代办用户id
        location: null
    },
    onLoad(options) {
        if (options[0]) {
            this.data.redirectType = options[0]
        }
        if (options.memberId) {
            this.data.memberId = options.memberId
        }

        this.getSetting()
    },
    onPullDownRefresh() {
        this.setData({ list: [] });
        this.init();
        wx.stopPullDownRefresh();
    },
    init() {
        this.fetchList();
    },
    fetchList() {
        this.handleFetchData()
    },
    handleSearch(e) {
        this.data.apiField.search.Keywords = e.detail
        this.init()
    },
    handleInput(e) {
        if (e.detail.value) {
            this.data.apiField.search.Keywords = e.detail.value
            this.init()
        }
        else {
            this.setData({
                loaded: false,
                list: []
            })
        }
    },
    handleClear(e) {
        this.setData({
            loaded: false,
            list: []
        })
    },
    getLocation() {
        var that = this
        wx.getLocation({
            success(res) {
                console.log(res)
                that.data.apiField.search.Longitude = res.longitude
                that.data.apiField.search.Latitude = res.latitude
                that.init()
            },
            fail(res) {
                wx.showToast({
                    title: '授权失败',
                    icon: 'success',
                    duration: 1000
                })
            }
        })
    },
    getSetting() {
        var that = this
        wx.getSetting({
            success: (res) => {
                if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化进入该页面,且未授权
                    wx.showModal({
                        title: '是否授权当前位置',
                        content: '将获取您的位置用于显示附近车库信息，请确认授权！',
                        success: function (res) {
                            if (res.cancel) {
                                wx.showToast({
                                    title: '授权失败',
                                    icon: 'success',
                                    duration: 1000
                                })
                            } else if (res.confirm) {
                                wx.openSetting({
                                    success: function (dataAu) {
                                        if (dataAu.authSetting["scope.userLocation"] == true) {
                                            wx.showToast({
                                                title: '授权成功',
                                                icon: 'success',
                                                duration: 1000
                                            })
                                            //再次授权，调用getLocationt的API
                                            that.getLocation();
                                        } else {
                                            wx.showToast({
                                                title: '授权失败',
                                                icon: 'success',
                                                duration: 1000
                                            })
                                        }
                                    }
                                })
                            }
                        }
                    })
                } else if (res.authSetting['scope.userLocation'] == undefined) {//初始化进入
                    that.getLocation();
                }
                else { //授权后默认加载
                    that.getLocation();
                }
            }
        })
    },
    handleCall(e) {
        var phone = e.currentTarget.dataset.phone
        wx.makePhoneCall({
            phoneNumber: phone
        })
    },
    handleOpenLocation(e) {
        var item = e.currentTarget.dataset.item
        wx.openLocation({
            latitude: item.Latitude,
            longitude: item.Longitude
        })
    },
    handleToPage(e) {
        const { id } = e.currentTarget.dataset
        let page = "monthPriceList"
        if (this.data.redirectType == 1) {
            page = "addPowerCard"
        }
        wx.$Fun.toPage({
            page: page,
            params: {
                parkId: id,
                memberId: this.data.memberId
            }
        })
    }
}))